package edu.gatech.doi.spamframework.mapping;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;



public class PropertiesFileOperator {

	private String filename;
	private Properties defaultprops;
	
	public PropertiesFileOperator(String filename)
	{
		this.filename=filename;
	}
	
	public void load()
	{
		// create and load default properties
		this.defaultprops = new Properties();
		FileInputStream in;
		try {
			File file=new File(this.filename);
			in = new FileInputStream(file);
			this.defaultprops.load(in);
			
//			System.out.println("test:"+this.defaultprops.getProperty("messagemodelfilename"));
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}
	
	public void save()
	{
		File file=new File(this.filename);
		FileOutputStream out;
		try {
			out = new FileOutputStream(file);
			this.defaultprops.store(out, "---No Comment---");
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void saveAs(String filepath)
	{
		File file=new File(filepath);
		FileOutputStream out;
		try {
			out = new FileOutputStream(file);
			this.defaultprops.store(out, "---No Comment---");
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	public void dispose()
	{
		this.defaultprops=null;
	}
	
	public void setProperty(String key,String value)
	{
		this.defaultprops.setProperty(key, value);
	}
	
	public void setPropertyMap(HashMap hm)
	{
		this.defaultprops=new Properties();
		Iterator it=hm.keySet().iterator();
		while(it.hasNext())
		{
			String key=(String) it.next();
			
			String value=(String) hm.get(key);
			this.defaultprops.put(key, value);
		}
		
	}
	
	public void remove(Object key)
	{
		this.defaultprops.remove(key);
		
	}
	
	public boolean contains(Object value)
	{
		return this.defaultprops.contains(value);
	}
	
	public boolean containsKey(Object key)
	{
		return this.defaultprops.containsKey(key);
	}
	
	public boolean containsValue(Object value)
	{
		return this.defaultprops.containsValue(value);
	}
	
	public String getProperty(String key)
	{
		return this.defaultprops.getProperty(key);
	}
	
	public HashMap getPropertyMap()
	{
		HashMap hm=new HashMap();
		Enumeration<Object> e=this.defaultprops.keys();
		while(e.hasMoreElements())
		{
			String key=(String) e.nextElement();
			String value=this.defaultprops.getProperty(key);
			hm.put(key, value);
		}
		return hm;
	}
	public HashMap getPropertyMap(String keypart)
	{
		HashMap hm=new HashMap();
		Enumeration<Object> e=this.defaultprops.keys();
		while(e.hasMoreElements())
		{
			
			String key=(String) e.nextElement();
			if(key.contains(keypart))
			{
				
				String value=this.defaultprops.getProperty(key);
				hm.put(key, value);
			}
			
		}
		return hm;
	}
	
	public void deleteProperty(String key)
	{
		if(this.defaultprops.containsKey(key))
		{
			this.defaultprops.remove(key);
		}
	}
	
	public void deleteAllProperty()
	{
		this.defaultprops=new Properties();
	}
	
	public void modifyProperty(String key,String newvalue)
	{
		if(this.defaultprops.containsKey(key))
		{
			this.defaultprops.remove(key);
			this.defaultprops.put(key, newvalue);
		}
	}
	
	public void addProperty(String key,String value)
	{
		this.defaultprops.put(key, value);
	}
	
	
}
